from typing import List

from app.services.business.gaolu_diagnose.ind.abstract_ind import AbstractInd


class CG_LT_GL_GL04_shaojiekZB(AbstractInd):

    def get_name(self):
        return "CG_LT_GL_GL04_shaojiekZB"

    def get_data_source(self):
        return "mysql"

    def get_sql(self, params):

        sql_shaojiekhao = """
        SELECT
        SUM(fqty) as shaojiekhao               
        FROM
        syn_tq_mbom1
        INNER JOIN syn_tq_mmain ON syn_tq_mbom1.fitemid = syn_tq_mmain.fitemid
        WHERE
        syn_tq_mbom1.finsid = 8  and fqty >0 and fbomtype=4 and ftypeb=1  
        and ftypec=1 and  fcreattime > DATE_SUB(CURTIME(), INTERVAL 1.1 hour)
        """.format(**params)

        sql_kuanghao = """
        SELECT
        SUM(fqty) as kuanghao               
        FROM
        syn_tq_mbom1
        INNER JOIN syn_tq_mmain ON syn_tq_mbom1.fitemid = syn_tq_mmain.fitemid
        WHERE
        syn_tq_mbom1.finsid = 8  and fqty >0 and fbomtype=4 and ftypeb=1  
        and  fcreattime > DATE_SUB(CURTIME(), INTERVAL 1.1 hour)
        """.format(**params)
        return [sql_shaojiekhao, sql_kuanghao]

    @AbstractInd.cache_wrapper
    def get_result(self):
        sql_list: List[str] = self.get_sql(self.params)
        cursor = self.mysql_cursor

        shaojiekhao = sql_list[0]
        cursor.execute(shaojiekhao)
        shaojiekhao = cursor.fetchone()
        shaojiekhao = float(shaojiekhao['shaojiekhao']) if shaojiekhao['shaojiekhao'] is not None else 0

        kuanghao = sql_list[1]
        cursor.execute(kuanghao)
        kuanghao= cursor.fetchone()
        kuanghao= float(kuanghao['kuanghao']) if kuanghao['kuanghao'] is not None else 0

        if shaojiekhao == 0 or kuanghao == 0: return 0
        return round(shaojiekhao / kuanghao, 2)
